<?php


use Plugins\Demo\Http\Admin\Controller\IndexController;
use Sc\Util\HtmlStructure\Html\Html;
use Sc\Util\HtmlStructure\Html\Js;
use Sc\Util\HtmlStructure\Html\Js\VueComponents\Temporary;
use Sc\Util\HtmlStructure\Table;

Html::create('基础表格加基础事件');

$table = Table::create(
    // 第一个参数为数据接口地址， sc_route函数为封装的路由函数，自动吧控制器的方法地址转为url
    sc_route([IndexController::class, 'demoListData']),
    // 第二个参数为表格ID，用于区分多个表格
    'demo2'
)->addColumns(
    Table\Column::selection(),
    Table\Column::normal('ID', 'id'),
    Table\Column::normal('姓名', 'name'),
    Table\Column::normal('年龄', 'age'),
    Table\Column::normal('状态', 'status'),
    Table\Column::normal('简介', 'intro'),
    Table\Column::normal('创建时间', 'created_at'),
);

// 刷新数据。this.demo1GetData() 为获取数据,格式为 tableId + GetData
$table->setHeaderEvent('@primary.Refresh.刷新', 'this.demo2GetData()');
$table->setHeaderEvent('@primary.Plus.添加', function () {
    return Table\EventHandler::window("添加")->setConfig(['width' => '1000px'])->setUrl(
        sc_route([IndexController::class, 'demoForm'])
    );
});

$temporary = Temporary::create("demo2component")->setContent(include __DIR__ . "/demo_form.sc.php");
$table->setHeaderEvent('@primary.Plus.添加(组件模式)', function () use($temporary){
    return Table\EventHandler::window("添加(组件模式)")->setConfig(['width' => '1000px'])->setComponent(
        $temporary
    );
});

$table->setRowEvent('@primary.Edit.修改(组件模式)', function () use($temporary){
    return Table\EventHandler::window("修改(组件模式)")->setConfig(['width' => '1000px'])->setComponent(
        $temporary
    );
});

$table->setHeaderEvent("@danger.delete.删除",
    Table\EventHandler::post(
        url: sc_route([IndexController::class, 'demoDel']),
        data: ['ids' => '@selection.map(v => v.id)'],
        successHandler: Js::code("this.\$message.success('删除成功')")->then("this.demo2GetData()")
    )->confirmMessage('确认删除选中数据吗？')
        ->addLoading()
);


return $table->render();